package com.yinhe.music.yhmusic.localmusic.view;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.TabAdapter;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * @author 何晓华
 */
public class LocalActivity extends BaseServiceActivity {
    @BindView(R.id.viewpager)
    ViewPager mViewPager;
    @BindView(R.id.tabs)
    TabLayout tabLayout;
    private String[] title = { "单曲", "歌手","专辑","文件夹"};
    private boolean loadDone;

    @Override
    public void initView() {
        setToolbar("本地音乐");
        showQuickControl(true);
        if (CacheManager.getInstance().isLoadingSuccess()){
            setTab();
        }
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_local;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void initPresenter() {

    }

    private void setTab() {
        ViewUtils.reflexIndicator(tabLayout, 20);
        tabLayout.setupWithViewPager(mViewPager);
        initDownLoadAdapter();
        loadDone = true;
    }

    private void initDownLoadAdapter() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(LocalMusicFragment.newInstance(false));
        fragmentList.add(LocalSingerFragment.newInstance());
        fragmentList.add(LocalAlbumbFragment.newInstance());
        fragmentList.add(LocalFileFragment.newInstance());

        TabAdapter mAdapter = new TabAdapter(getSupportFragmentManager(),fragmentList,title);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setBackgroundColor(getResources().getColor(R.color.white));
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = @Tag(RxBusEventType.Music.SCAN_MUSIC)
    )
    public void onRxbusScanMusic(RxbusNullObject rxbusNullObject){
        if (CacheManager.getInstance().isLoadingSuccess() && !loadDone){
            setTab();
        }
    }



}
